package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallDrawActivityPrizePoolDetail;
import com.sc.nft.mapper.MallDrawActivityPrizePoolDetailMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MallDrawActivityPrizePoolDetailDao extends ServiceImpl<MallDrawActivityPrizePoolDetailMapper, MallDrawActivityPrizePoolDetail> {

    public boolean changePrizeCount(Long id, Integer count, boolean deduct) {
        LambdaUpdateWrapper<MallDrawActivityPrizePoolDetail> updateWrapper = new LambdaUpdateWrapper<>();
        if (deduct) {
            updateWrapper.setSql("remaining_prize_count = remaining_prize_count - " + count);
        } else {
            updateWrapper.setSql("remaining_prize_count = remaining_prize_count + " + count);
        }
        updateWrapper.eq(MallDrawActivityPrizePoolDetail::getId, id);
        updateWrapper.ge(deduct, MallDrawActivityPrizePoolDetail::getRemainingPrizeCount, count);
        return update(updateWrapper);
    }

    public boolean changeTotalPrizeCount(Long id, Integer count, boolean deduct) {
        LambdaUpdateWrapper<MallDrawActivityPrizePoolDetail> updateWrapper = new LambdaUpdateWrapper<>();
        if (deduct) {
            updateWrapper.setSql("total_prize_count = total_prize_count - " + count);
        } else {
            updateWrapper.setSql("total_prize_count = total_prize_count + " + count);
        }
        updateWrapper.eq(MallDrawActivityPrizePoolDetail::getId, id);
        updateWrapper.ge(deduct, MallDrawActivityPrizePoolDetail::getTotalPrizeCount, count);
        return update(updateWrapper);
    }

    public List<MallDrawActivityPrizePoolDetail> getByPrizePoolId(Long prizePoolId) {
        LambdaQueryWrapper<MallDrawActivityPrizePoolDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallDrawActivityPrizePoolDetail::getPoolId, prizePoolId);
        return list(queryWrapper);
    }

    public Boolean removeByPrizePoolId(Long poolId) {
        LambdaQueryWrapper<MallDrawActivityPrizePoolDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallDrawActivityPrizePoolDetail::getPoolId, poolId);
        return this.remove(wrapper);

    }

}
